package com.chixing.service.impl;

import com.chixing.config.RabbitMQConfiguration;
import com.chixing.mapper.CouponMapper;
import com.chixing.pojo.Coupon;
import com.chixing.pojo.CouponExample;
import com.chixing.service.CouponService;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public ServerResponse getByStatus(Integer cusId, Integer coupon_status) {
        CouponExample example = new CouponExample();
        example.createCriteria().andCouponStatusEqualTo(coupon_status).andCusIdEqualTo(cusId);
        List<Coupon> couponList = couponMapper.selectByExample(example);
        if (couponList != null && couponList.size()>0){
            return ServerResponse.getSuccess(couponList);
        }

        return ServerResponse.getFail(null);

    }

    @Override
    public ServerResponse getById(Integer couponId) {
        Coupon coupon = couponMapper.selectByPrimaryKey(couponId);
        if (coupon !=null){
            return ServerResponse.getSuccess(coupon);
        }else
            return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse save(Coupon coupon) {

        coupon.setCouponOther1(UUID.randomUUID().toString());

        int rows = couponMapper.insertSelective(coupon);
        if (rows > 0) {
            this.rabbitTemplate.convertAndSend(
                    RabbitMQConfiguration.couponExchange, //发送至订单交换机
                    RabbitMQConfiguration.routingKeycoupon, //订单定routingKey
                    coupon//订单号 这里可以传对象 比如直接传订单对象
                    , message -> {
                        message.getMessageProperties().setExpiration(1000 * 10 + "");

                        return message;
                    });
            return ServerResponse.saveSuccess(rows);
        }
        return ServerResponse.saveFail(null);
    }

    @Override
    public ServerResponse update(Coupon coupon) {
        int rows = couponMapper.updateByPrimaryKey(coupon);
        if (rows>0)
            return ServerResponse.updateSuccess(rows);
        return ServerResponse.updateFail(null);
    }

    @Override
    public ServerResponse delete(Integer couId) {
        int rows = couponMapper.deleteByPrimaryKey(couId);
        if (rows>0)
            return ServerResponse.deleteSuccess(rows);
        return ServerResponse.deleteFail(null);
    }

    @Override
    public ServerResponse getByPage(Integer cusId, Integer coupon_status, Integer pageNum) {
        CouponExample example = new CouponExample();
        example.createCriteria().andCouponStatusEqualTo(coupon_status).andCusIdEqualTo(cusId);
        List<Coupon> couponList = couponMapper.selectByExample(example);
        if (couponList !=null && couponList.size()>0){
            PageHelper.startPage(pageNum,4);
            PageInfo<Coupon> couponPageInfo = new PageInfo<>(couponList);
            return ServerResponse.getSuccess(couponPageInfo);
        }
        return ServerResponse.getFail(null);
    }
    @Override
    public Coupon getByKey(String couponOther1) {
        if(couponOther1 !=null){
            CouponExample couponExample = new CouponExample();
            couponExample.createCriteria().andCouponOther1EqualTo(couponOther1);
            Coupon coupon = couponMapper.selectByExample(couponExample).get(0);
            return coupon;
        }else
            return null;

    }
}
